package com.demo.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@Slf4j
public class YanShiConfig {



    @Value("${delay.normal.queue}")
    private String delayNormalQueue;
    @Value("${delay.normal.exchange}")
    private String delayNormalTopicExchange;
    @Value("${delay.queue}")
    private String delayDeadQueue;
    @Value("${delay.exchange}")
    private String delayDeadExchange;



    @Bean
    public Queue delayNormalQueue() {

        Map<String, Object> arguments = new HashMap<>();
        // 绑定死信交换机
        arguments.put("x-message-ttl", 1000*5); // 队列过期 10秒后过期
        arguments.put("x-dead-letter-exchange", "delayTopicExchange");
        // 绑定死信的路由key
        arguments.put("x-dead-letter-routing-key", "delayQueueRoutingKey"+".#");

        return new Queue("delayNormalQueue",true,false,false,arguments);
    }


    @Bean
    TopicExchange normalExchange() {
        return new TopicExchange("delayNormalTopicExchange");
    }


    @Bean
    Binding bindingNormalQueue() {
        return BindingBuilder.bind(delayNormalQueue()).to(normalExchange()).with("delayNormalQueueRoutingKey"+".#");
    }


    //延时队列和交换器
    @Bean
    public Queue delayQueue() {
        return new Queue("delayQueue");
    }

    @Bean
    TopicExchange delayTopicExchange() {
        return new TopicExchange("delayTopicExchange");
    }

    @Bean
    Binding bindingDelayQueue() {
        return BindingBuilder.bind(delayQueue()).to(delayTopicExchange()).with("delayQueueRoutingKey"+".#");
    }



}
